#coding: utf-8
from time import sleep
import re,sys,common
import serial 
global ser
execfile('..\import.cfg')
port = upgrade_test_port
frist_upgrade = upgrade_frist_time
second_upgrade = upgrade_second_time
def check_upgrade_is_ok(com,timeout):
    out_str = ''
    count = 0
    global ser
    while True:
        try:
            read_buf=com.read(300)
            out_str = out_str + read_buf
        except:
            print 'read fail'
            while True:
                try:
                    print 'again open port'
                    ser = serial.Serial(port=port,baudrate=115200,timeout=1) 
                    break
                except:
                    sleep(5)
                    continue
        if out_str.find("UPGRADEIND"):
            code_list=re.findall('UPGRADEIND: (-[0-9]+)',out_str)
            if code_list:
                print 'upgrade error'
                print out_str
                return False
        if (out_str.find("UPGRADEVER: 1") != -1) or (out_str.find("UPGRADEVER: 0") != -1):
            print(out_str)
            return True
        sleep(0.1)
        if out_str:
            print out_str
            out_str = ''
        count = count + 1
        if count > timeout*10:
            return False

def check_at_is_ok(com):
    out_str = ''
    count = 1
    while True:
        read_buf=ser.read(300)
        out_str = out_str+read_buf
        if out_str.find('OK') != -1:
            print out_str
            return True
        sleep(0.1)
        count = count+1
        if count>300:
            print out_str
            return False
def main():
    global ser
    while True: 
        ser = common.check_com_is_ok(port)
        print 'ATE1\r\n'
        ser.write('ATE1\r\n')
        if not check_at_is_ok(ser):
            print 'AT check error'
        sleep(1)
        #改变productkey
        ser.write('AT+UPGRADE=KEY,'+productKey+'\r\n')
        if not check_at_is_ok(ser):
            print 'AT check error'
        sleep(1)
        #测试1小时的远程升级周期
        print 'test 1 hour upgrade'
        ser.write('AT+UPGRADE=PERIOD,'+str(frist_upgrade)+'\r\n')
        if not check_at_is_ok(ser):
            print 'AT check error'
        #检查远程升级是否成功
        if not check_upgrade_is_ok(ser,4000):
            common.quit()  
        print 'test 1 hour upgrade ok'
        print 'test 5 hour upgrade'
        print 'ATE1\r\n'
        ser.write('ATE1\r\n')
        sleep(2)
        #测试5小时的远程升级周期
        ser.write('AT+UPGRADE=PERIOD,'+str(second_upgrade)+'\r\n')
        #检查远程升级是否成功
        if not check_upgrade_is_ok(ser,5*3600+400):
            common.quit() 
        print 'test 5 hour upgrade ok'
        print 'hand upgrade start'
        #手动升级测试
        ser.write('ATE1\r\n')
        if common.check_net_is_ok(ser):
            ser.write('AT+UPGRADE\r\n')
        if not check_upgrade_is_ok(ser,400):
            print 'hand upgrade error'
            common.quit() 
        ser.close()
        sleep(5*60)
        print 'hand upgrade sucesss'
        print 'test ok'
sys.stdout = common.Log()      
main()